package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.admin.business.inputOrderReback.ProductOfUnConnectTableDto;
import com.linkai.entity.Storage;
import com.linkai.mapper.StorageMapper;
import com.linkai.mapper.SupplierMapper;
import com.linkai.service.StorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 库存 服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-02-27
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {

    @Autowired
    private SupplierMapper supplierMapper;

    /**
     * 根据商品id更新数据
     *
     *
     * @param id
     * @param eachSum
     * @param bstoId
     * @param supId
     * @param inpordPrice
     * @return int
     */
    @Override
    public int updateStorageByProId(Integer id, Integer productNum, BigDecimal eachSum, Integer bstoId, Integer supId, BigDecimal inpordPrice) {
        return baseMapper.updateStorageByProId(id,productNum,eachSum,bstoId,supId,inpordPrice);
    }

    /**
     * 根据商品id更新商品数量，减去商品数量
     *
     * @param id  id
     * @param num 数量
     * @return flag
     */
    @Override
    public boolean updateProductNumByProId(Integer id, Integer num,BigDecimal eachSum,BigDecimal price,Integer stoId,Integer supId) {
        return baseMapper.updateProductNumByProId(id,num,eachSum,price,stoId,supId);
    }

    /**
     * 根据仓库id查找非关联商品的table
     *
     * @param stoId 仓库id
     * @return list
     */
    @Override
    public List<ProductOfUnConnectTableDto> listProduct4UnConnected(Integer stoId) {
        List<ProductOfUnConnectTableDto> list = new ArrayList<>();
        final QueryWrapper<Storage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bsto_id",stoId);
        final List<Storage> storages = baseMapper.selectList(queryWrapper);
        for (Storage storage : storages) {
            final ProductOfUnConnectTableDto productOfUnConnectTableDto = new ProductOfUnConnectTableDto();
            BeanUtils.copyProperties(storage,productOfUnConnectTableDto);
            productOfUnConnectTableDto.setSupName(supplierMapper.selectById(productOfUnConnectTableDto.getSupId()).getSupName());
            list.add(productOfUnConnectTableDto);
        }
        return list;
    }

}
